package com.lt.controller.stu;

import com.lt.dto.ProjectApply;
import com.lt.entity.Student;
import com.lt.service.ProjectApplyService;
import com.lt.service.StuAndTeacherService;
import com.lt.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Author :liteng
 * @Time : 2023/10/30 15:15
 */

@Slf4j
@RestController
@RequestMapping("/stu")
@CrossOrigin
public class ProjectApplyController {

    private final StuAndTeacherService stuAndTeacherService;
    private final ProjectApplyService projectApplyService;

    public ProjectApplyController(StuAndTeacherService stuAndTeacherService, ProjectApplyService projectApplyService) {
        this.stuAndTeacherService = stuAndTeacherService;
        this.projectApplyService = projectApplyService;
    }

    /**
     * 项目申请
     * @param appliedProject
     * @return
     */
    @PostMapping("/apply")
    public Result projectApply(@RequestBody ProjectApply appliedProject) {

        log.info(String.valueOf(appliedProject));
        if (appliedProject != null) {
            Long projectId = projectApplyService.addProject(appliedProject);
            return Result.success(projectId);
        }
        return Result.error("参数错误");
    }

    /**
     * 根据学号查询学生
     * @param stuNum
     * @return
     */
    @GetMapping("num/{stuNum}")
    public Result findStu(@PathVariable("stuNum") Long stuNum) {
        Student stu = stuAndTeacherService.findByStuNum(stuNum);
        if (stu == null) {
            return Result.error("学号输入错误，未查询到学生");
        }
        return Result.success(stu);
    }
}
